package 力扣;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-08-14
 * Time: 23:14
 */
//最多能完成排序的块
public class max_chunks_to_make_sorted_ii {
    public static int maxChunksToSorted(int[] arr) {
        Stack<Integer> stack = new Stack<>();
        for(int x : arr){
            int t = x;
            while(!stack.empty() && stack.peek() > x){
                t = Math.max(t, stack.peek());
                stack.pop();
            }
            stack.push(t);
        }
        return stack.size();
    }

    public static void main(String[] args) {
        int[] array = {2,1,3,4,4};
        System.out.println(maxChunksToSorted(array));
    }
}
